package com.cwd.arm.application.service.read;

import com.cwd.arm.infrastructure.dao.ProjectMonthAmountTotalDao;
import com.cwd.arm.infrastructure.po.ProjectMonthAmountTotalPo;
import com.cwd.arm.application.dto.ProjectMonthAmountTotalDto;
import com.cwd.arm.application.service.read.facade.ProjectMonthAmountTotalReadService;
import com.cwd.arm.infrastructure.query.ProjectMonthAmountTotalDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
* Description:项目每月统计
* Created on 2021年11月16日
* @author  yan
*/
@Repository
@Primary
public class ProjectMonthAmountTotalReadServiceImpl implements ProjectMonthAmountTotalReadService {

    @Autowired
    private ProjectMonthAmountTotalDao projectMonthAmountTotalDao;

    @Override
    public ProjectMonthAmountTotalDto selectById(Long id) {
        // TODO: 21-11-15
        return convertToDto(null);
    }

    @Override
    public List<ProjectMonthAmountTotalDto> selectByProjectId(Long projectId) {
        ProjectMonthAmountTotalDaoQuery daoQuery = new ProjectMonthAmountTotalDaoQuery();
        daoQuery.setProjectId(projectId);
        return selectByQuery(daoQuery);
    }

    @Override
    public List<ProjectMonthAmountTotalDto> selectByProjectIdYear(Long projectId, int Year) {
        ProjectMonthAmountTotalDaoQuery projectMonthAmountTotalDaoQuery = new ProjectMonthAmountTotalDaoQuery();
        projectMonthAmountTotalDaoQuery.setFyYear(Year);
        projectMonthAmountTotalDaoQuery.setProjectId(projectId);
        return selectByQuery(projectMonthAmountTotalDaoQuery);
    }

    @Override
    public ProjectMonthAmountTotalDto selectByYearAndMonth(Long projectId, int year, int month) {
        return convertToDto(projectMonthAmountTotalDao.selectOne(projectId,year,month));
    }

    @Override
    public List<ProjectMonthAmountTotalDto> selectByQuery(ProjectMonthAmountTotalDaoQuery daoQuery) {
        List<ProjectMonthAmountTotalPo> poList = projectMonthAmountTotalDao.selectByQuery(daoQuery);
        return convertToDtoList(poList);
    }

    private ProjectMonthAmountTotalDto convertToDto(ProjectMonthAmountTotalPo po){
        if(po == null){
            return null;
        }
        ProjectMonthAmountTotalDto dto = new ProjectMonthAmountTotalDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<ProjectMonthAmountTotalDto> convertToDtoList(List<ProjectMonthAmountTotalPo> poList){
        return poList.stream().map(po->convertToDto(po)).collect(Collectors.toList());
    }

}
